﻿"use strict";

Number.prototype.decimal = function (numdecimalpoints, totalwidth)
{
    var retval = this.toFixed(numdecimalpoints);
    if (totalwidth)
        retval = retval.padleft(' ', totalwidth);
    return retval;
}
Number.prototype.padleft = function (padchar, length)
{
    var space = '';
    for (var i = 0; i < length; i++)
        space += padchar;
    return (space + this).slice(-length);
}
Number.prototype.padright = function (padchar, length)
{
    var space = '';
    for (var i = 0; i < length; i++)
        space += padchar;
    return (this + space).substring(0, length);
}
String.prototype.padleft = function (padchar, length)
{
    var space = '';
    for (var i = 0; i < length; i++)
        space += padchar;
    return (space + this).slice(-length);
}
String.prototype.padright = function (padchar, length)
{
    var space = '';
    for (var i = 0; i < length; i++)
        space += padchar;
    return (this + space).substring(0, length);
}
String.prototype.startsWith = function (str)
{
    return this.indexOf(str) == 0;
};
Number.prototype.clamp = function (min, max)
{
    return Math.min(Math.max(this, min), max);
};

function CreateCanvas(width, height, fillcolor)
{
    var img = document.createElement('canvas');
    img.width = width;
    img.height = height;
    var ctx = img.getContext('2d');
    ctx.fillStyle = fillcolor;
    ctx.fillRect(0, 0, width, height);
    return img;
}

function Copy(toobject, fromobject)
{
    if (fromobject)
    {
        for (var key in fromobject)
        {
            if (fromobject.hasOwnProperty(key))
                toobject[key] = fromobject[key];
        }
    }
    return toobject;
}

function Cryo(key, value)
{
    try
    {
        if (value instanceof Object)
        {
            if (value instanceof Image)
                value = null
            else if ('toDataURL' in value)
                value = null;
            else if ('_class' in value)
                value._class = value._class;
            if (value instanceof Sprite)
                value.path = null;
            else if (value instanceof TileMap)
            {
                value.walkmap = null;
                value.cache = {};
                value.colors = null;
            }
        }
        return value;
    } catch (ex)
    {
        log('Error when freezing ' + key + ' ' + value.toString + ' ' + ex);
    }
}

function Revive(key, value)
{
    if (value instanceof Object)
    {
        if (false)
            ;
        else if (value._class == 'TileMap')
            value = new TileMap(value);
        else if (value._class == 'Background')
            value = new Background(value);
        else if (value._class == 'CursorSprite')
            value = new CursorSprite(value);
        else if (value._class == 'ImageSprite')
            value = new ImageSprite(value);
        else if (value._class == 'Label')
            value = new Label(value);
        else if (value._class == 'Button')
            value = new Button(value);
        else if (value._class == 'TextSprite')
            value = new TextSprite(value);
    }
    // default to returning the value unaltered
    return value;
}

function Save(obj)
{
    try
    {
        var s = JSON.stringify(obj, Cryo);
        console.log(s);
        localStorage.setItem(obj.id, s);
        alert(obj.id + ' ' + obj.name + ' saved to local storage.');
        log(obj.id + ' ' + obj.name + ':\n' + s + '\n');
    }
    catch (ex)
    {
        log('Error when saving ' + obj.id + ' - ' + ex.message);
    }
}
function Load(objid)
{
    var obj = JSON.parse(localStorage.getItem(objid), Revive);
    if (obj == null)
    {
        alert('Could not load ' + objid + ' from local storage.');
        return null;
    }
    else
    {
        log(objid + ' ' + obj.name + ' loaded from local storage.');
        return obj;
    }
}
